package com.bridge.baselibs.rxpremissions;

import android.Manifest;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.functions.Consumer;
import static android.Manifest.permission.ACCESS_FINE_LOCATION;
import static android.Manifest.permission.CAMERA;
import static android.Manifest.permission.READ_PHONE_STATE;
import static android.Manifest.permission.RECORD_AUDIO;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
/**
 * 权限申请功能模块
 */
public class PermissionsUtils {

    /**
     * 检查权限
     *
     * @param activity
     * @param permissions 权限名称
     * @param onNext      反馈
     */
    public static void checkPermission(FragmentActivity activity, String permissions, Consumer<Boolean> onNext) {
        RxPermissions rxp = new RxPermissions(activity);
        rxp.request(permissions).subscribe(onNext);
    }

    public static void checkPermission(Fragment fragment, String permissions, Consumer<Boolean> onNext) {
        RxPermissions rxp = new RxPermissions(fragment);
        rxp.request(permissions).subscribe(onNext);
    }

    public static void showPermissionDialog(Context context, String permissions, OnClickListener cancelClick) {
        showPermissionDialog(context, permissions, null, cancelClick);
    }

    /**
     * 显示权限申请消息
     *
     * @param context
     * @param permissions 权限名称
     * @param cancelClick 取消
     */
    public static void showPermissionDialog(Context context, String permissions, String _features, OnClickListener cancelClick) {
        String content = null;
        String appName = "警综";
        String tips = "先点权限行不行";
        String features = _features;

        if (TextUtils.equals(permissions, WRITE_EXTERNAL_STORAGE)) {
            String permissName =WRITE_EXTERNAL_STORAGE;
            content = String.format(tips, appName, permissName, appName);
        } else if (TextUtils.equals(permissions, READ_PHONE_STATE)) {
            String permissName = READ_PHONE_STATE;
            String setting_method = String.format(tips, appName, permissName, appName);
            String explain = READ_PHONE_STATE;
            content = String.format(explain, appName, appName, setting_method);
        } else if (TextUtils.equals(permissions, ACCESS_FINE_LOCATION)) {
            String permissName = ACCESS_FINE_LOCATION;
            if (null == features)
                features = ACCESS_FINE_LOCATION;
            content = String.format(tips, appName, permissName, features);
        } else if (TextUtils.equals(permissions, CAMERA)) {
            String permissName = CAMERA;
            if (null == features)
                features =CAMERA;
            content = String.format(tips, appName, permissName, features);
        } else if (TextUtils.equals(permissions, RECORD_AUDIO)) {
            String permissName = RECORD_AUDIO;
            if (null == features)
                features = RECORD_AUDIO;
            content = String.format(tips, appName, permissName, features);
        }
        showDialog(context, content, cancelClick);
    }

    private static void showDialog(final Context context, String content, OnClickListener cancelClick) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("请求使用一些权限");
        builder.setMessage(content);
        builder.setCancelable(false);
        builder.setPositiveButton("去设置", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent();
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setAction(Settings.ACTION_APPLICATION_SETTINGS);
                context.startActivity(intent);
            }
        }).setNegativeButton("取消", cancelClick);
        builder.create().show();
    }
    public static void requestAllPermissions(FragmentActivity activity) {
        RxPermissions rxPermission = new RxPermissions(activity);
        rxPermission.requestEach(
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.CALL_PHONE,
                Manifest.permission.CAMERA,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.NFC,
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.WAKE_LOCK,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_WIFI_STATE,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.CHANGE_WIFI_STATE,
                Manifest.permission.INTERNET,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.BLUETOOTH,
                Manifest.permission.REQUEST_INSTALL_PACKAGES
        ).subscribe(permission -> {
            if (permission.granted) {
                // 用户已经同意该权限
                Log.d("RxPermissions", permission.name + " is granted.");
            } else if (permission.shouldShowRequestPermissionRationale) {
                // 用户拒绝了该权限，没有选中『不再询问』（Never ask again）,那么下次再次启动时。还会提示请求权限的对话框
                Log.d("RxPermissions", permission.name + " is denied. More info should be provided.");
            } else {
                // 用户拒绝了该权限，而且选中『不再询问』
                Log.d("RxPermissions",permission.name + " is denied.");
            }

        });
    }
}
